package com.myapp.config;

import com.gitee.fastmybatis.core.query.LambdaQuery;
import com.gitee.fastmybatis.core.support.Getter;

import java.util.Collection;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * @author 六如
 */
public class MyQuery<T> extends LambdaQuery<T> {

    public MyQuery(Class<T> entityClass) {
        super(entityClass);
    }


    @Override
    public LambdaQuery<T> in(Getter<T, ?> getter, Collection<?> value) {
        // 去重
        Set<Object> uniqueValues = new HashSet<>(value);
        return super.in(getter, uniqueValues);
    }

    @Override
    public LambdaQuery<T> eq(Getter<T, ?> getter, Object value) {
        // 兼容Collection
        if (value instanceof Collection) {
            return this.in(getter, (Collection<?>) value);
        }
        // 兼容布尔
        if (Objects.equals(value, true)) {
            return super.eq(getter, 1);
        }
        if (Objects.equals(value, false)) {
            return super.eq(getter, 0);
        }
        // 兼容枚举
        if (value instanceof IEnum) {
            return super.eq(getter, ((IEnum<?>) value).getValue());
        }

        return super.eq(getter, value);
    }
}
